package com.crecheng.shop.controller;

import com.alibaba.fastjson.JSON;
import com.crecheng.shop.DAO.UserCartData;
import com.crecheng.shop.Manage.UserCartManage;
import com.crecheng.shop.util.MyMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/userCart")
public class UserCartController {

    @RequestMapping("/get")
    public @ResponseBody
    String getUserCart(@RequestParam(value = "uid",defaultValue = "-1")int uid,
                       @RequestParam(value = "id",defaultValue = "-1")int id){
        if(uid<1)
            return MyMessage.defaultSqlError;
        else {
            if(id<0)
                return JSON.toJSONString(UserCartManage.getByUser(uid));
            else
                return JSON.toJSONString(UserCartManage.get(id));
        }
    }

    @RequestMapping("/del")
    public @ResponseBody
    String delUserCart(@RequestParam(value = "uid",defaultValue = "-1")int uid,
                       @RequestParam(value = "id",defaultValue = "-1")int id){
        if(uid<1)
            return MyMessage.defaultSqlError;
        else {
            if(id<0)
                return MyMessage.toJson("delCount",UserCartManage.delByUser(uid));
            else {
                if(UserCartManage.del(id))
                    return MyMessage.defaultSuccess;
                else
                    return MyMessage.defaultSqlError;
            }
        }
    }

    @RequestMapping("/add")
    public @ResponseBody
    String add(@RequestParam(value = "uid",defaultValue = "-1")int uid,
               @RequestParam(value = "sid",defaultValue = "-1")int sid,
               @RequestParam(value = "gid",defaultValue = "-1")int gid,
               @RequestParam(value = "num",defaultValue = "-1")int num){
        if(uid<1||sid<1||gid<1||num<1)
            return MyMessage.defaultError;
        UserCartData data=new UserCartData();
        data.set(uid,sid,gid,num);
        if(UserCartManage.add(data))
            return MyMessage.defaultSuccess;
        else
            return MyMessage.defaultSqlError;
    }

    @RequestMapping("/update")
    public @ResponseBody
    String update(@RequestParam(value = "id",defaultValue = "-1")int id,
               @RequestParam(value = "sid",defaultValue = "-1")int sid,
               @RequestParam(value = "gid",defaultValue = "-1")int gid,
               @RequestParam(value = "num",defaultValue = "-1")int num){
        if(id<1||sid<1||gid<1||num<1)
            return MyMessage.defaultError;
        var d=UserCartManage.get(id);
        if (d != null) {
            d.set(d.getUid(),sid,gid,num);
            if(UserCartManage.update(d))
                return MyMessage.defaultSuccess;
            else
                return MyMessage.defaultSqlError;
        }
            return MyMessage.defaultSqlNotFoundError;
    }

    @RequestMapping("/changeNum")
    public @ResponseBody
    String changeNum(@RequestParam(value = "id",defaultValue = "-1")int id,
                     @RequestParam(value = "addNum",defaultValue = "0")int num){
        if(id<1||num==0)
            return MyMessage.defaultError;
        if(UserCartManage.changeNum(id,num))
            return MyMessage.defaultSuccess;
        else
            return MyMessage.defaultSqlError;
    }

}
